#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import os
from datetime import datetime
from .._util import Util, WinFile
from ._spec import Spec


class BatchFile(WinFile):

    def __init__(self, ts):
        spec = Spec()

        self._ts = ts

        # the following options and default values are from:
        #   1. Microsoft Batch 98 (it is contained in win98 installation CD)
        #   2. https://www.tek-tips.com/viewthread.cfm?qid=612507

        # "General Setup Options / Install Info" tab in application "Microsoft Batch 98"
        self.productKey = None
        self.installationDirectory = r"c:\windows"          # strange that it is in lower case
        self.bShowInstallationDirectoryWarning = False
        self.bAutoCreateUninstallInformation = True

        # "General Setup Options / User Info" tab in application "Microsoft Batch 98"
        self.userName = None
        self.companyName = None
        self.computerName = None
        self.workgroup = None
        self.description = None

        # "General Setup Options / Setup Prompts" tab in application "Microsoft Batch 98"
        self.bAutoAcceptEndUserLicenseAgreement = True       # no effect for OEM cdrom?
        self.bPromptForEmergencyStartupDisk = False
        self.bAutoRebootPciAndPnpMachinesDuringSetup = True
        self.bSearchForSourceFolderForNewDevices = False
        self.bSkipThePccardWizard = True

        # "General Setup Options / Regional Settings" tab in application "Microsoft Batch 98"
        pass

        # "General Setup Options / Desktop" tab in application "Microsoft Batch 98"
        self.bShowDesktopIconDict = {k: True for k in spec.getOptionalDesktopIconList()}
        self.bDeleteOnlineServiceFromDesktopAfterSetup = False
        self.bShowTheWindows98WelcomeScreen = True
        self.bShowTheWindowsRegistrationWizard = True

        # "General Setup Options / Printers" tab in application "Microsoft Batch 98"
        pass                                                    # FIXME

        # "General Setup Options / MRU Locations" tab in application "Microsoft Batch 98"
        pass                                                    # FIXME

        # "General Setup Options / User Profiles" tab in application "Microsoft Batch 98"
        pass

        # "Network Options / Protocols" tab in application "Microsoft Batch 98"
        self.bIpx = False
        self.bIpxNetBios = False
        self.bNetBeui = False
        self.bMsDlc = False
        self.bTcpIp = True

        # "Network Options / Services" tab in application "Microsoft Batch 98"
        pass

        # "Network Options / Clients" tab in application "Microsoft Batch 98"
        pass

        # "Network Options / Access Control" tab in application "Microsoft Batch 98"
        pass

        # "Network Options / Additional Clients" tab in application "Microsoft Batch 98"
        pass

        # "Optional Components" in application "Microsoft Batch 98"
        self.bOptionalComponentDict = spec.getOptionalComponentDefaultValueDict().copy()

        # FIXME: "Internet Explorer Options" in application "Microsoft Batch 98"
        pass

        # "Advanced Options / Additional Files" tab in application "Microsoft Batch 98"
        pass

        # "Advanced Options / Windows Update" tab in application "Microsoft Batch 98"
        self.bEnableUpgradeOfDriversViaTheInternet = True
        self.bEnableUpgradeOfSoftwareViaTheInternet = True

        #
        self.bExtraDrivers = False

    def get_filepath(self):
        return "win98/msbatch.inf"

    def get_content(self):
        spec = Spec()
        selfFilename = os.path.basename(self.get_filepath())

        # assign values
        self.productKey = self._ts.product_key
        self.installationDirectory = spec.getWindowsDir()

        self.userName = self._ts.user_name
        self.workgroup = "WORKGROUP"                                # use a fixed name

        self.bDeleteOnlineServiceFromDesktopAfterSetup = True       # fully unattended, FIXME no effect
        self.bShowTheWindows98WelcomeScreen = False                 # fully unattended
        self.bShowTheWindowsRegistrationWizard = False              # fully unattended

        self.bIpx = self._ts.ipx
        self.bIpxNetBios = self._ts.ipx
        self.bNetBeui = self._ts.ipx                                       # FIXME: old and bad protocol
        self.bMsDlc = self._ts.ipx                                         # FIXME: old and bad protocol
        self.bTcpIp = self._ts.tcp_ip

        # FIXME: dependence process
        for name in self.bOptionalComponentDict:
            if name in self._ts.optional_components_blacklisted:
                self.bOptionalComponentDict[name] = False
            if name in self._ts.optional_components_needed:
                self.bOptionalComponentDict[name] = True

        for name in self.bShowDesktopIconDict:
            if name in self._ts.optional_desktop_icons_blacklisted:
                self.bShowDesktopIconDict[name] = False
            if name in self._ts.optional_desktop_icons_needed:
                self.bShowDesktopIconDict[name] = True

        self.bEnableUpgradeOfDriversViaTheInternet = False          # win98 is too old, no update available anymore
        self.bEnableUpgradeOfSoftwareViaTheInternet = False         # win98 is too old, no update available anymore

        # generate
        buf = ""
        buf += "[BatchSetup]\n"                         # this is standard, and advpack.dll,LaunchINFSection needs it
        buf += "Version=3.0 (32-bit)\n"
        buf += "SaveDate=%s\n" % (datetime.now().strftime("%y/%m/%d"))
        buf += "\n"
        buf += "[Version]\n"                            # this is standard, and advpack.dll,LaunchINFSection needs it
        buf += 'Signature = "$CHICAGO$"\n'
        buf += "AdvancedINF=2.5\n"
        buf += "LayoutFile=layout.inf\n"
        buf += "\n"

        buf += "[Setup]\n"
        buf += "Express=1\n"                            # what does it mean?
        buf += 'ProductKey="%s"\n' % (self.productKey)
        buf += 'InstallDir="%s"\n' % (self.installationDirectory)
        buf += "InstallType=3\n"                        # what does it mean?
        buf += "EBD=%s\n" % (Util.boolTo01(self.bPromptForEmergencyStartupDisk))
        buf += "ShowEula=%s\n" % (Util.boolTo01(not self.bAutoAcceptEndUserLicenseAgreement))
        buf += "ChangeDir=0\n"                          # what does it mean?
        buf += "OptionalComponents=1\n"                 # what does it mean?
        buf += "Network=1\n"                            # what does it mean?
        buf += "System=0\n"                             # what does it mean?
        buf += "CCP=0\n"                                # what does it mean?
        buf += "CleanBoot=1\n"                          # what does it mean??
        buf += "Display=0\n"                            # what does it mean?
        buf += "DevicePath=%s\n" % (Util.boolTo01(not self.bSearchForSourceFolderForNewDevices))
        buf += "NoDirWarn=%s\n" % (Util.boolTo01(not self.bShowInstallationDirectoryWarning))
        buf += 'TimeZone="%s"\n' % (self._ts.timezone)
        buf += "Uninstall=%s\n" % (Util.boolTo01(self.bAutoCreateUninstallInformation))
        buf += "NoPrompt2Boot=%s\n" % (Util.boolTo01(self.bAutoRebootPciAndPnpMachinesDuringSetup))     # seems no effect
        # buf += "VRC=0\n"
        # buf += "PenWinWarning=0\n"
        buf += "\n"

        buf += "[System]\n"
        # buf += 'Display="VBE Miniport"\n' ; Comes from vbemp.inf
        # buf += 'Monitor="QEMU Monitor"\n'
        buf += "DisplChar=%s\n" % (",".join(self._convertScreenInfo(spec.getScreenInfoBootstrap())))
        buf += "Locale=%s\n" % (spec.getLocaleValue(self._ts.locale))
        buf += "SelectedKeyboard=%s\n" % (spec.getKeyboardLayoutValue(self._ts.keyboard_layout))
        buf += "\n"

        buf += "[NameAndOrg]\n"
        if self.userName is not None:
            buf += 'Name="%s"\n' % (self.userName)
        if self.companyName is not None:
            buf += 'Org="%s"\n' % (self.companyName)
        buf += "Display=0\n"
        buf += "\n"

        buf += "[Network]\n"
        if self.computerName is not None:
            buf += 'ComputerName="%s"\n' % (self.computerName)
        if self.workgroup is not None:
            buf += 'Workgroup="%s"\n' % (self.workgroup)
        if self.description is not None:
            buf += 'Description="%s"\n' % (self.description)
        buf += "Display=0\n"
        buf += "PrimaryLogon=VREDIR\n"                              # be dynamic?
        buf += "Clients=VREDIR\n"                                   # be dynamic?
        if self.bIpx or self.bNetBeui or self.bMsDlc or self.bTcpIp:
            protoList = []
            if self.bNetBeui:
                protoList.append("NETBEUI")
            if self.bIpx:
                protoList.append("NWLINK")
                if self.bIpxNetBios:
                    protoList.append("NWNBLINK")    # NetBIOS over IPX/SPX
            if self.bTcpIp:
                protoList.append("MSTCP")
            if self.bMsDlc:
                protoList.append("MSDLC32")
            buf += "Protocols=%s\n" % (", ".join(protoList))
            if self.bTcpIp:
                buf += "DefaultProtocol=MSTCP\n"
            elif self.bIpx and not self.bIpxNetBios:
                buf += "DefaultProtocol=NWLINK\n"
            elif self.bIpx and self.bIpxNetBios:
                buf += "DefaultProtocol=NWNBLINK\n"
            else:
                assert False
        buf += "Services=VSERVER\n"                                 # what does it mean?
        buf += "Security=SHARE\n"                                   # what does it mean?
        buf += "\n"

        # vnetsup.vxd
        # vredir.vxd
        # dfs.vxd
        # vnetbios.vxd
        # msnp32.dll

        if self.bIpx:
            buf += "[NWLINK]\n"
            buf += "Frame_Type=4\n"

        if self.bTcpIp:
            buf += "[MSTCP]\n"
            buf += "DNS=0\n"            # don't use static DNS config
            buf += "DHCP=1\n"           # use DHCP, don't use static IP config
            buf += "WINS=D\n"           # use DHCP for WINS resolution
            buf += "LMHOSTS=0\n"        # don't use LMHost File for WINS
            buf += "\n"

        buf += "[VREDIR]\n"                                         # what does it mean?
        buf += 'LogonDomain="Home"\n'                               # what does it mean?
        buf += "ValidatedLogon=0\n"                                 # what does it mean?
        buf += "\n"

        buf += "[VSERVER]\n"
        buf += "LMAnnounce=0\n"
        buf += "MaintainServerList=2\n"

        buf += "[OptionalComponents]\n"
        for k, v in self.bOptionalComponentDict.items():
            buf += '"%s"=%s\n' % (k, Util.boolTo01(v))
        buf += "\n"

        buf += "[Printers]\n"
        buf += "\n"

        buf += "[InstallLocationsMRU]\n"
        buf += "\n"

        buf += "[Install]\n"
        if True:
            addRegItem = [
                "RunOnce.BatchDelay",
                "Run.Installed.Components",
            ]
            if self.bSkipThePccardWizard:
                addRegItem.append("Skip.PCMCIA.Wizard")
            if True:
                addRegItem.append("Registry.WinUpdate")
            if len(self._ts.extra_drivers) > 0:
                addRegItem.append("ExtraDrivers")
            # addRegItem += [
            #     "PreSaveSettings",
            #     "PostSaveSettings",
            # ]
            buf += "AddReg=%s\n" % (",".join(addRegItem))
        buf += "\n"
        if True:
            buf += "[RunOnce.BatchDelay]\n"
            i = 1
            if not self.bShowDesktopIconDict["My Documents"]:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.Mydocs.Icon 4 %10%\\' + selfFilename + '"\n'
                i += 1
            if not self.bShowDesktopIconDict["Network Neighbourhood"]:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.Nethood.Icon 4 %10%\\' + selfFilename + '"\n'
                i += 1
            if not self.bShowDesktopIconDict["Recycle Bin"]:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.Recycle.Icon 4 %10%\\' + selfFilename + '"\n'
                i += 1
            if not self.bShowDesktopIconDict["Setup the Microsoft Network"]:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.MSN.Icon 4 %10%\\' + selfFilename + '"\n'
                i += 1
            if not self.bShowTheWindows98WelcomeScreen:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.Welcome 4 %10%\\' + selfFilename + '"\n'
                i += 1
            if not self.bShowTheWindowsRegistrationWizard:
                buf += 'HKLM,%KEY_RUNONCE%,BatchRun' + str(i) + ',,"%25%\\rundll.exe setupx.dll,InstallHinfSection Delete.Regwiz 4 %10%\\' + selfFilename + '"\n'
                i += 1
            buf += "\n"
            if not self.bShowDesktopIconDict["My Documents"]:
                buf += "[Delete.Mydocs.Icon]\n"
                buf += "AddReg=Mydocs.Icon\n"
                buf += "\n"
                if True:
                    buf += "[Mydocs.Icon]\n"
                    buf += r"HKCU,Software\Microsoft\Windows\CurrentVersion\Explorer\Documents,HideMyDocsFolder,," + "\n"
                    buf += "\n"
            if not self.bShowDesktopIconDict["Network Neighbourhood"]:
                buf += "[Delete.Nethood.Icon]\n"
                buf += "AddReg=Nethood.Icon\n"
                buf += "\n"
                if True:
                    buf += "[Nethood.Icon]\n"
                    buf += r"HKCU,Software\Microsoft\Windows\CurrentVersion\Policies\Explorer,NoNetHood,1,01,00,00,00" + "\n"
                    buf += "\n"
            if not self.bShowDesktopIconDict["Recycle Bin"]:
                buf += "[Delete.Recycle.Icon]\n"
                buf += "DelReg=Recycle.Icon\n"
                buf += "\n"
                if True:
                    buf += "[Recycle.Icon]\n"
                    buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion\explorer\Desktop\Namespace\{645FF040-5081-101B-9F08-00AA002F954E},,,,,," + "\n"
                    buf += "\n"
            if not self.bShowDesktopIconDict["Setup the Microsoft Network"]:
                buf += "[Delete.MSN.Icon]\n"
                buf += "DelReg=MSN.Icon\n"
                buf += "\n"
                if True:
                    buf += "[MSN.Icon]\n"
                    buf += r"HKLM,SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Desktop\NameSpace\{4B876A40-4EE8-11D1-811E-00C04FB98EEC},,," + "\n"
                    buf += "\n"
            if not self.bShowTheWindows98WelcomeScreen:
                buf += "[Delete.Welcome]\n"
                buf += "DelReg=Registry.Welcome\n"
                buf += "\n"
                if True:
                    buf += "[Registry.Welcome]\n"
                    buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion\Run,Welcome,," + "\n"
                    buf += "\n"
            if not self.bShowTheWindowsRegistrationWizard:
                buf += "[Delete.Regwiz]\n"
                buf += "AddReg=Registry.Regwiz\n"
                buf += "\n"
                if True:
                    buf += "[Registry.Regwiz]\n"
                    buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion\Welcome\Regwiz,@,1,01,00,00,00" + "\n"
                    buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion,RegDone,1,01,00,00,00" + "\n"
                    buf += "\n"
        if True:
            # %25% means c:\windows\system, %10% means c:\windows
            buf += "[Run.Installed.Components]\n"
            if True:
                if True:
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchSetupx,,,">Batch 98 - General Settings"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchSetupx,IsInstalled,1,01,00,00,00\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchSetupx,Version,,"3,0,0,0"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchSetupx,StubPath,,"%25%\\rundll.exe setupx.dll,InstallHinfSection Installed.Components.General 4 %10%\\' + selfFilename + '"\n'     # refers to [Installed.Components.General] below
                if True:
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchAdvpack,,,">Batch 98 - Advanced Settings"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchAdvpack,IsInstalled,1,01,00,00,00\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchAdvpack,Version,,"3,0,0,0"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchAdvpack,StubPath,,"%25%\\rundll32.exe advpack.dll,LaunchINFSection %10%\\' + selfFilename + ',Installed.Components.Advanced"\n'      # refers to [Installed.Components.Advanced] below
                if True:
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchWu,,,">Batch 98 - Windows Update"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchWu,IsInstalled,1,01,00,00,00\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchWu,Version,,"3,0,0,0"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\>BatchWu,StubPath,,"wupdmgr.exe -shortcut"\n'
                if True:
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\BatchSetupx,,,"ICW"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\BatchSetupx,IsInstalled,1,01,00,00,00\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\BatchSetupx,Version,,"3,0,0,0"\n'
                    buf += 'HKLM,%KEY_INSTALLEDCOMPS%\\BatchSetupx,StubPath,,"%24%\\progra~1\\intern~1\\connec~1\\icwconn1 /restoredesktop\n'      # ?
                buf += "\n"
            if True:
                buf += "[Installed.Components.General]\n"
                buf += "AddReg=%s\n" % (",".join(
                    [
                        "Browser.Settings",                         # needed?
                        "Shell.Prep",                               # what does it mean?
                    ]
                ))
                buf += "BitReg=Shell.Settings\n"
                buf += "\n"
                if True:
                    buf += "[Browser.Settings]\n"
                    buf += 'HKCU,%KEY_IE_MAIN%,"Start Page",,"about:blank"\n'
                    buf += 'HKCU,%KEY_IE_MAIN%,"First Home Page",,"about:blank"\n'
                    buf += 'HKCU,%KEY_IE_MAIN%,"Search Page",,"http://www.google.com"\n'
                    buf += 'HKCU,%KEY_IE_MAIN%,"Search Bar",,"http://www.google.com"\n'
                    buf += 'HKCU,%KEY_IE_HELP%,,"Online_Support",,"http://support.microsoft.com/support"\n'
                    buf += "\n"
                if True:
                    buf += "[Shell.Prep]\n"
                    # HKCU,Software\Microsoft\Windows\CurrentVersion\Explorer,ShellState,1,1C,00,00,00,20,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,0A,00,00,00
                    buf += r'HKCU,"Software\Microsoft\Internet Explorer\Desktop\Components\0",Flags,01,00,00,00' + '\n'
                    buf += r'HKCU,"Software\Microsoft\Internet Explorer\main",Show_ChannelBand,0,"no"' + '\n'
                    buf += "\n"
                if True:
                    buf += "[Shell.Settings]\n"
                    buf += r'HKCU,"Software\Microsoft\Internet Explorer\Desktop\Components\0",Flags,0,20,1' + '\n'
                    # HKCU,Software\Microsoft\Windows\CurrentVersion\Explorer,ShellState,1,40,4
                    buf += "\n"
            if True:
                buf += "[Installed.Components.Advanced]\n"
                if False:
                    delFiles = [
                        "QuickLaunch.Icons",
                    ]
                    if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                        delFiles.append("OLS.Icons")
                    if not self.bShowDesktopIconDict["Connect to Internet"]:
                        delFiles.append("Connect.Icon")
                    buf += "DelFiles=%s\n" % (",".join(delFiles))
                if False:
                    buf += "CustomDestination=Custom.Dest\n"
                if False:
                    cmds = []
                    if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                        cmds.append("DirCleanup")
                    if len(cmds) > 0:
                        buf += "RunPostSetupCommands=%s\n" % (",".join(cmds))
                buf += "\n"
                if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                    buf += "[OLS.Icons]\n"
                    buf += "americ~1.lnk\n"
                    buf += "at&two~1.lnk\n"
                    buf += "compus~1.lnk\n"
                    buf += "prodig~1.lnk\n"
                    buf += "themic~1.lnk\n"
                    buf += "aboutt~1.lnk\n"
                    buf += "abouto~1.txt\n"
                    buf += "services.txt\n"
                    buf += "\n"
                if True:
                    buf += "[QuickLaunch.Icons]\n"
                    buf += "viewch~1.scf\n"
                    buf += "\n"
                if not self.bShowDesktopIconDict["Connect to Internet"]:
                    buf += "[Connect.Icon]\n"
                    buf += "connec~1.lnk\n"
                    buf += "\n"
                if True:
                    buf += "[Custom.Dest]\n"
                    if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                        buf += "49070=DesktopLDIDSection,5\n"
                    if True:
                        buf += "49050=QuickLinksLDIDSection,5\n"
                    buf += "\n"
                    if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                        buf += "[DesktopLDIDSection]\n"
                        buf += r'HKCU,"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders",Desktop,OLSFolder,"%25%\Desktop"' + '\n'
                        buf += "\n"
                    if True:
                        buf += "[QuickLinksLDIDSection]\n"
                        buf += r'HKCU,"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders",AppData,QuickLaunch,"%25%\Application Data"' + '\n'
                        buf += "\n"
                if self.bDeleteOnlineServiceFromDesktopAfterSetup:
                    buf += "[DirCleanup]\n"
                    buf += r"rundll32.exe advpack.dll,DelNodeRunDLL32 %49070%\%OnlineLong%" + "\n"
                    buf += "\n"
        if self.bSkipThePccardWizard:
            buf += "[Skip.PCMCIA.Wizard]\n"
            buf += r"HKLM,System\CurrentControlSet\Services\Class\PCMCIA,SkipWizardForBatchSetup,,1" + "\n"
            buf += "\n"
        if True:
            buf += "[Registry.WinUpdate]\n"
            buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion\Policies\Explorer,NoDevMgrUpdate,0x10001,%s" % (Util.boolTo01(not self.bEnableUpgradeOfDriversViaTheInternet)) + "\n"
            buf += r"HKLM,Software\Microsoft\Windows\CurrentVersion\Policies\Explorer,NoWindowsUpdate,0x10001,%s" % (Util.boolTo01(not self.bEnableUpgradeOfSoftwareViaTheInternet)) + "\n"
            buf += "\n"
        if len(self._ts.extra_drivers) > 0:
            buf += "[ExtraDrivers]\n"
            # buf += 'HKLM,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion","OtherDevicePath",,"%s"\n' % (";".join(["C:\\DRIVERS\\" + x for x in self._ts.extra_drivers]))
            buf += r'HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion","OtherDevicePath",,"C:\WINDOWS\SYSTEM\PRECOPY"' + "\n"
            buf += "\n"
        if True:
            # installs programs before the Save Settings stage.
            buf += "[PreSaveSettings]\n"
            if False:
                buf += r'HKLM,%KEY_RUNONCESETUP%,"Windows Media Player 9",,"C:\Windows\setup\wmp\MPLAY9.exe /Q"' + "\n"
            if False:
                buf += r'HKLM,%KEY_RUNONCESETUP%,"Media Player Codecs",,"C:\Windows\setup\wmp\CODECS9x.exe /Q"' + "\n"
            buf += r'HKLM,%KEY_RUNONCESETUP%,"System Updates (1 of 4)",,"C:\WINDOWS\SYSTEM\PRECOPY\PreSave.bat"' + "\n"
        if True:
            # installs programs after the Save Settings stage. If you wish to install IE 6 sp1 during setup, use this section to install it. You CANNOT use PreSaveSettings
            buf += "[PostSaveSettings]\n"
            if False:
                buf += r'HKLM,%KEY_RUNONCE%,"Internet Explorer 6 sp1",,"C:\Windows\setup\IEsetup\ie6setup.exe /Q:A /R:N"' + "\n"
            buf += r'HKLM,%KEY_RUNONCE%,"Post-Save Routines",,"C:\WINDOWS\SYSTEM\PRECOPY\PostSave.bat"' + "\n"

        # who uses this?
        buf += "[DelOEdesktop]\n"
        buf += r'setup.ini, progman.groups,, "groupOE=..\..\desktop"' + '\n'
        buf += r'setup.ini, groupOE,, """Outlook Express"""' + '\n'                 # delete outlook express icon?
        buf += "\n"

        # global section
        buf += "[DestinationDirs]\n"
        if not self.bShowDesktopIconDict["Connect to Internet"]:
            buf += "Connect.Icon=10,alluse~1\\desktop\n"
        if self.bDeleteOnlineServiceFromDesktopAfterSetup:
            buf += "OLS.Icons=49070,Online~1\n"
        buf += r"QuickLaunch.Icons=49050,Micros~1\Intern~1\QuickL~1" + "\n"
        buf += "\n"

        # [RunOnce.PaschkeRuntimes]
        # HKLM,%KEY_RUNONCE%,BatchVBRUN60,,"%25%\rundll32.exe advpack.dll,LaunchINFSection C:\vbrun60\vbrun60.inf,DefaultInstall"
        # HKLM,%KEY_RUNONCE%,BatchSHORTCUT,,"%13%\cscript.exe C:\SETSTRT.VBS"
        # HKLM,%KEY_RUNONCE%,BatchCLEAN,,"%25%\rundll32.exe advpack.dll,DelNodeRunDLL32 C:\vbrun60"

        # [RegistrySettings]
        # ; The registry files must be located in the
        # ; Windows 98 installation directory.
        # HKLM,%KEY_RUNONCE%,BatchReg1,,"%25%\regedit.exe /s %1%\science.reg"
        # HKLM,%KEY_RUNONCE%,BatchReg2,,"%25%\regedit.exe /s %1%\viewhidd.reg"

        # if True:
        #     buf += "[OPKInstall]\n"
        #     buf += "; The following line allows ACPI on all OEM machines regardless of the good BIOS list\n"
        #     buf += r'HKLM,Software\Microsoft\Windows\CurrentVersion\Detect,"ACPIOption",,1' + '\n'
        #     buf += "; This gets you past the EULA stage\n"
        #     buf += 'HKLM,%KEY_CURR_VER%,"ProductId",,"' + '16305-OEM-0088921-12491' + '"\n'
        #     buf += 'HKLM,%KEY_CURR_VER%,"ProductKey",,"' + self.productKey + '"\n'
        #     buf += 'HKLM,%KEY_CURR_VER%,"RegisteredOwner",,"' + self.userName + '"\n'
        #     buf += 'HKLM,%KEY_CURR_VER%,"RegisteredOrganization",,"' + 'ORG_NAME' + '"\n'
        #     buf += "; This gets you past the password stage\n"
        #     buf += r'HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Network\Real Mode Net","autologon",1,00000000' + '\n'
        #     buf += "\n"
        # buf += r'KEY_RUN="SOFTWARE\Microsoft\Windows\CurrentVersion\Run"' + '\n'

        # global section
        buf += "[Strings]\n"
        buf += r'KEY_RUNONCE="SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"' + '\n'
        buf += r'KEY_RUNONCESETUP="SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\Setup"' + '\n'
        buf += r'KEY_INSTALLEDCOMPS="SOFTWARE\Microsoft\Active Setup\Installed Components"' + '\n'
        buf += r'KEY_IE_MAIN="SOFTWARE\Microsoft\Internet Explorer\Main"' + '\n'
        buf += r'KEY_IE_HELP="SOFTWARE\Microsoft\Internet Explorer\Help_Menu_URLs"' + '\n'
        buf += r'KEY_CURR_VER="SOFTWARE\Microsoft\Windows\CurrentVersion"' + '\n'                   # needed?
        if self.bDeleteOnlineServiceFromDesktopAfterSetup:
            buf += r'OnlineLong="Online Services"' + '\n'
        buf += "\n"

        buf += "; Always make sure your .inf, .bat and .reg files have at least one carriage return\n"
        buf += "; at the end or the file may just not work at all. For Reg files, none of it will work.\n"
        buf += "; No wonder NASA space missions fail with just one wrong hyphen.\n"
        buf += "\n"

        return buf

    def _convertScreenInfo(self, screenInfo):
        return (str(screenInfo[2]), str(screenInfo[0]), str(screenInfo[1]))
